<template>
  <div class="login_box">
    <!-- 
        model:表单数据对象
         v-model：数据绑定

        rules:表单验证规则
        prop:定义用那个验证

    -->
    <el-form :model="user" :rules="rules" label-width="100px" ref="form" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="user.username" ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="goLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      user: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods:{
    //去登录
    goLogin(){
        //1.必须是验证通过了才能登录（必填，满足要求）
        //validate:对整个表单进行验证，判断是否验证成功
        //语法：form.validate
        this.$refs.form.validate(val => {
            //val : boolean  true:成功  false：失败
            if(!val){  //验证没有通过不能往下执行
                return;
            }

            //发起ajax请求，去登录

            //路由跳转
            this.$router.push("/index/home")
        })
    }
  }
};

/* 
  布局：element找带验证的表单
  绑定数据：和后台要的结构字段一样
  制定验证规格
  点击登录--登录
    验证通过才能登录--- 验证
    跳转首页
*/ 


</script>
  
  <style scoped>
.login_box {
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.2);
}
.el-form {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-70%, -50%);
}
</style>